package com.up.upzl.base.config;

import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class NetUtil {

	public static CloseableHttpClient httpClient = HttpClientBuilder.create().build();

	/**
	 * get请求获取String类型数据
	 * 
	 * @param url
	 *            请求链接
	 * @return
	 */
	public static String get(String url) {
		StringBuffer sb = new StringBuffer();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = httpClient.execute(httpGet); // 1
			HttpEntity entity = response.getEntity();
			InputStreamReader reader = new InputStreamReader(entity.getContent(), "utf-8");
			char[] charbufer;
			while (0 < reader.read(charbufer = new char[10])) {
				sb.append(charbufer);
			}
		} catch (IOException e) {// 1
			e.printStackTrace();
		} finally {
			httpGet.releaseConnection();
		}
		return sb.toString();
	}
}